package cn.gdchent.springbootsecurity.enity;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;

/**
 * @auther:gdchent
 * @create:2020/4/20 16:22
 * @Description:
 */
public class UserInfoDetails implements UserDetails {

    private String username;  //用户名
    private String password;  // 密码
    //private String roles;   //用户的权限集合 用逗号隔开

    boolean accountNonExpired=true;   //是否没过期
    boolean accountNonLocked=true;   //是否没被锁定
    boolean credentialsNonExpired=true;  //是否没过期
    boolean enabled=true;  //账号是否可用
    List<? extends GrantedAuthority> authorities;  //用户的权限集合

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    /**
     * 从数据库中取出权限字符串进行分解 返回一个集合
     * @return
     */
//    @Override
//    public Collection<? extends GrantedAuthority> getAuthorities() {
//        //
//        String [] authorities=roles.split(",");
//        List<SimpleGrantedAuthority> simpleGrantedAuthorities=new ArrayList<>();
//        for(String role:authorities){
//             simpleGrantedAuthorities.add(new SimpleGrantedAuthority(role));
//        }
//        return simpleGrantedAuthorities;
//    }


    public void setAuthorities(Collection<? extends GrantedAuthority> authorities) {
        this.authorities = (List<? extends GrantedAuthority>) authorities;
    }

    @Override
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }



    public void setUsername(String username) {
        this.username = username;
    }

    @Override
    public String getUsername() {
        return username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return accountNonExpired;
    }

    @Override
    public boolean isAccountNonLocked() {
        return accountNonLocked;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return credentialsNonExpired;
    }

    @Override
    public boolean isEnabled() {
        return enabled;
    }


}
